﻿<?php
class sscls_ProductController extends sscls_Controller
{
    function __construct()
    {
        $this->view='product';
    }
	
	function view()
	{
		$model=$this->getModel();
		$cat=$model->getCategories();
		$session=ss_Main::getSession();
		$catids=$session->get('catids');
		if(isset($_POST['catids']))
		{
			$catid=$_POST['catids'];
			$session->set('catids',$catid);
		}
		else
		if(@$catids!=''||@$catids!='')
		{
			$catid=$catids;
		}else
			$catid=$cat[0]->id;
			
		$values['attri']=$model->getAttri($catid);
		$values['product']=$model->getProducts($catid);
		$values['model']=$model;
		$values['cat']=$cat;
		$values['catid']=$catid;
		$this->layout='products';
		$this->view='product';
		$this->display($values);
		return;
	}
	
	function attri()
	{
		$model=$this->getModel();
		$values['attries']=$model->getAttries();
		$this->layout='attries';
		$this->view='product';
		$this->display($values);
		return;
	}
	
	function addattri()
	{	
		
		$model=$this->getModel();
		$vals['cat']=$model->getCategories();
		$id=sscls_Request::getRequest('id');
		$vals['item']=$model->getAttriItem($id);
		$this->layout='addattri';
		$this->view='product';
		$this->display($vals);
		
	}
	
	function proccessaddattri()
	{
		$post=sscls_Request::get('post');
		$user=ss_Main::getUser();
		if($post['name']=='')
		{
			$this->setRedirec('index.php?c=product&t=addattri&id='.$post['id'],'Vui lòng nhập tên thuộc tính.','error');
			return;
		}
		$post['created_by']=$user->id;
		$post['created_date']=strtotime('now');
		$post['id']=sscls_Request::getRequest('id');
		$model=$this->getModel();
		
		if($model->store1($post,'product_attri'))
		{
			$this->setRedirec('index.php?c=product&t=attri','Đã lưu thành công.');
			return;
		}
		$this->setRedirec('index.php?c=product&t=addattri&id='.$post['id'],'Đã xảy ra lỗi trong quá trình lưu. Vui lòng thử lại.','error');
		return;
	}
	
    function add()
	{			
		$model=$this->getModel();
		
		
		
			
		$vals['model']=$model;
		$vals['cat']=$model->getKindMoto();
		$id=sscls_Request::getRequest('id');
		$vals['item']=$model->getItem($id);
		if(isset($_POST['catid']))
		{
			$vals['attri']=$model->getAttri($_POST['catid']);
			$vals['model1']=$model->getModel($_POST['catid']);
		}else
		if(isset($_GET['id'])&&@$_GET['id']!=0&&!isset($_POST['catid']))
		{
			$vals['attri']=$model->getAttri($vals['item']->catid);
			$vals['model1']=$model->getModel($vals['item']->catid);
		}else
		if(!isset($vals['item']->catid))
		{
			$vals['attri']=$model->getAttri($vals['cat'][0]->id);
			$vals['model1']=$model->getModel($vals['cat'][0]->id);
		}
		
	
		
		
		$this->layout='add';
		$this->view='product';
		$this->display($vals);
		
	}
	function removeattri()
	{
		$cid=$_POST['ids'];
		$model=$this->getModel();
		$model->removeattri($cid);
		$this->setRedirec('index.php?c=product&t=attri');
		return;
	}
	
	function removeproduct()
	{
		$cid=$_POST['ids'];
		$model=$this->getModel();
		if($model->removeproduct($cid))
		{
			foreach((array)$cid as $productid)
			{
				$model->removeAttriByProductId($productid);
			}
			
		}
		$this->setRedirec('index.php?c=product');
		return;
	}
	function proccessadd()
	{
		$post=sscls_Request::get('post');
		$user=ss_Main::getUser();
		$post['created_by']=$user->id;
		if($post['productname']=='')
		{
			$this->setRedirec('index.php?c=product&t=add&id='.(int)$post['id'],'Vui lòng nhập tên sản phẩm.','error');
			return;
		}
		
		if($post['mode']=='')
		{
			$this->setRedirec('index.php?c=product&t=add&id='.(int)$post['id'],'Vui lòng nhập model sản phẩm.','error');
			return;
		}
		
		$model=$this->getModel();
		if($model->store1($post,'products'))
		{
			if((int)$post['id']>0)
				$productid=$post['id'];
			else
				$productid=$model->getID();
				
			$model->removeAttriByProductId($productid);
			$data=array();
			foreach((array)$post['idattri'] as $t=>$idattri)
			{
				$data['idattri']=$idattri;
				$data['product_id']=$productid;
				$data['value']=$post['attrivalue'][$t];
				$model->store1($data,'product_attri_value');
			}
			$this->setRedirec('index.php?c=product','Cập nhật thành công');
			return;
		}
		$this->setRedirec('index.php?c=product&t=add&id='.$post['id'],'Đã có lỗi trong quá trình lưu. Vui lòng thử lại','error');
		return;
	}
	
	function showimage()
	{
		$model=$this->getModel();
		$productid=sscls_Request::getRequest('productid');
		$images=$model->getImages($productid);
		$values['images']=$images;
		$this->layout='images';
		$this->view='product';
		$this->display($values);
		return;
	}
	
	function addimage()
	{
		$model=$this->getModel();
		$imageid=sscls_Request::getRequest('id');
		$image=$model->getImage($imageid);
		$values['image']=$image;
		$this->layout='addimage';
		$this->view='product';
		$this->display($values);
		return;
	}
	
	function processaddimage()
	{
		$post=sscls_Request::get('post');
		$images=$this->upload('images/products/','index.php?c=product&t=addimage&id='.$post['id'].'&productid='.$post['productid']);
		include('controllers/classresize.php');
		
	   $image = new SimpleImage();
	   if($post['resize']==1){
	   $image->load('images/products/'.$images);
	   $image->resize((int)$post['width'],(int)$post['height']);
	   $image->save('images/products/'.$images);
	   }
	   
	   $image->load('images/products/'.$images);
	   $image->resize(75,75);
	   $image->save('images/products/'.'thum'.$images);
		if($images!=null&&$images!='')
		{
			$post['thumnail']='thum'.$images;
			$post['image_name']=$images;
		}
		
		if($post['title']=='')
		{
			$this->setRedirec('index.php?c=product&t=addimage&id='.$post['id'].'&productid='.$post['productid'],'Vui lòng nhập tên sản phẩm.','error');
			return;
		}
		
		
		$model=$this->getModel();
		if($model->store1($post,'product_images'))
		{
			
			$this->setRedirec('index.php?c=product&t=showimage&productid='.$post['productid'],'Cập nhật thành công');
			return;
		}
		$this->setRedirec('index.php?c=product&t=addimage&id='.$post['id'].'&productid='.$post['productid'],'Đã có lỗi trong quá trình lưu. Vui lòng thử lại','error');
		return;
	}
	
	function upload($url,$urlback)
	{	
		$user=ss_Main::getUser();
		
		//define a maxim size for the uploaded images in Kb
		 define ("MAX_SIZE","10000"); 

		//This variable is used as a flag. The value is initialized with 0 (meaning no error  found)  
		//and it will be changed to 1 if an errro occures.  
		//If the error occures the file will not be uploaded.
		 $errors=0;
		//checks if the form has been submitted
		 if(isset($_POST['Submit'])) 
		 {
			//reads the name of the file the user submitted for uploading
			$image=$_FILES['image']['name'];
			//if it is not empty
			if ($image) 
			{
			//get the original name of the file from the clients machine
				$filename = stripslashes($_FILES['image']['name']);
			//get the extension of the file in a lower case format
				$extension = $this->getExtension($filename);
				$extension = strtolower($extension);
			//if it is not a known extension, we will suppose it is an error and will not  upload the file,  
			//otherwise we will do more tests
		 if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) 
				{
				//print error message
					$this->setRedirec($urlback,'Dịnh dạng file không hợp lệ','error');
					return null;
					$errors=1;
				}
				else
				{
		//get the size of the image in bytes
		 //$_FILES['image']['tmp_name'] is the temporary filename of the file
		 //in which the uploaded file was stored on the server
		 $size=filesize($_FILES['image']['tmp_name']);

		//compare the size with the maxim size we defined and print error if bigger
		if ($size > MAX_SIZE*1024)
		{
			$this->setRedirec($urlback,'Vui lòng chọn file kích thước nhỏ hơn','error');
			return null;
			$errors=1;
		}

		//we will give an unique name, for example the time in unix time format
		$image_name=md5($user->id).time().'.'.$extension;
		//the new name will be containing the full path where will be stored (images folder)
		$newname=$url.$image_name;
		//we verify if the image has been uploaded, and print error instead
		$copied = copy($_FILES['image']['tmp_name'], $newname);
		if (!$copied) 
		{
			$this->setRedirec($urlback,'Không thể coppy hình ảnh lên server. Bạn vui lòng kiểm tra phân quyền của thư mục cần đưa lên','error');
			return null;
			$errors=1;
		}}}}

		//If no errors registred, print the success message
		 
		return $image_name;
	}
	
	
	//This function reads the extension of the file. It is used to determine if the file  is an image by checking the extension.
	 function getExtension($str) {
			 $i = strrpos($str,".");
			 if (!$i) { return ""; }
			 $l = strlen($str) - $i;
			 $ext = substr($str,$i+1,$l);
			 return $ext;
	 }
	
	function removeimage()
	{
		$cid=$_POST['cid'];
		$model=$this->getModel();
		foreach((array)$cid as $ci)
		{
			$image=$model->getImage($ci);
			$url[]=PATH_ROOT.DS.'images'.DS.'products'.DS.$image->image_name;
		}
		
		if($model->removeImage($cid))
		{
			if(!$this->removeFile($url))
			{
				$this->setRedirec('index.php?c=product&t=showimage&productid='.$_POST['productid'],'Không thể xóa lưu trữ vật lý của hình vừa chọn. Vui lòng thử lại','error');
				return; 
			}
			$this->setRedirec('index.php?c=product&t=showimage&productid='.$_POST['productid']);
			return; 
		}
		$this->setRedirec('index.php?c=product','Đã có lỗi trong quá trình xóa file. Vui lòng thử lại.','error');
		return;
	}
		 
	function removeFile($url)
	{		
		foreach((array)$url as $ur)
		{
			@unlink($ur);
		}
		return true;
	}
    
    
}
 
?>